package com.cyt.web.controller.phone;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.*;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.PhoneController;
import com.cyt.work.association.model.po.WxAssociation;
import com.cyt.work.association.model.vo.WxAssociationVo;
import com.cyt.work.association.service.WxAssociationService;
import com.cyt.work.association.model.po.WxAssociationNew;
import com.cyt.work.association.model.vo.WxAssociationNewVo;
import com.cyt.work.association.service.WxAssociationNewService;
import com.cyt.work.association.model.po.WxAssociationSign;
import com.cyt.work.association.model.vo.WxAssociationSignVo;
import com.cyt.work.association.service.WxAssociationSignService;
import com.cyt.work.util.cache.AssassicationDetailUtils;
import com.cyt.work.association.model.vo.WxAssociationNewDetailVo;
import com.cyt.work.association.service.WxAssociationNewDetailService;
import com.cyt.work.association.model.vo.WxAssociationNewStuVo;
import com.cyt.work.association.service.WxAssociationNewStuService;
import com.cyt.wx.model.po.WxEducationUser;
import com.cyt.wx.model.po.WxMember;
import com.cyt.wx.model.vo.WxEducationUserVo;
import com.cyt.wx.service.WxEducationUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 社团报名
 *
 * @author han jun
 * @date 2018/12/15 11:26
 */
@RestController
public class PhoneAssociationController extends PhoneController {

    @Autowired
    private WxEducationUserService wxEducationUserService;

    @Resource
    private WxAssociationNewStuService wxAssociationNewStuService;

    @Resource
    private WxAssociationNewDetailService wxAssociationNewDetailService;

    @Resource
    private WxAssociationService wxAssociationService;

    @Resource
    private WxAssociationNewService wxAssociationNewService;

    @Resource
    private WxAssociationSignService wxAssociationSignService;



    /**
     * 登录页面
     * @param token
     * @param applyId
     * @author han jun
     * @date 2019/9/4 14:55
     */
    @RequestMapping(value = "/phone/association/associationLogin")
    public ModelAndView questionLogin(String token, String applyId) {
        ModelAndView modelAndView = new ModelAndView("/mobile/association/associationLogin");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        return modelAndView;
    }


    /**
     * 登录
     * @param token
     * @param applyId
     * @param loginName
     * @param password
     * @author  hanjun
     * @date 2019/9/4 16:26
     */
//    @BaseLog(module = "社团管理", classz = "社团管理", methods = "登录社团", businessType = BusinessType.INSERT, operatorType= OperatorType.MOBILE)
    @RequestMapping(value = "/phone/association/login",method = RequestMethod.POST)
    public ReturnResult login(String token, String applyId, String loginName, String password){
        WxMember wxMember = acceptToken(token);
        return wxEducationUserService.login(wxMember, loginName, password);
    }

    /**
     * 社团列表页面
     * @param token
     * @param applyId
     * @author cuibiming
     * @date 2019/9/9 08:50
     */
    @RequestMapping(value = "/phone/association/associationList")
    public ModelAndView associationList(String token, String applyId) {
        // 校验
        Map<String, Object> map = acceptToken(token, applyId, APPLY_TYPE_STZX);
        if (!(Boolean) map.get("flag")) {
            return (ModelAndView) map.get("modelAndView");
        }
        WxMember wxMember = acceptToken(token);
        WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
        wxEducationUserVo.setMemberId(wxMember.getId());
        List<WxEducationUser> wxEducationUserList = wxEducationUserService.listExample(wxEducationUserVo);
        //能登录进去就可以直接使用
        WxEducationUser wxEducationUser = wxEducationUserList.get(0);
        ModelAndView modelAndView =new ModelAndView();
        if(wxEducationUser.getUserType().equals(BaseConstant.EDUCATION_USER_TYPE_TEACHER)){
            modelAndView = new ModelAndView("/mobile/association/teacherAssociationList");
        }else if(wxEducationUser.getUserType().equals(BaseConstant.EDUCATION_USER_TYPE_STUDENT)) {
            modelAndView = new ModelAndView("/mobile/association/studentAssociationList");
        }
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        return modelAndView;
    }

    /**
     * 社团详情页面
     * @param token
     * @param applyId
     * @author cuibiming
     * @date 2019/9/9 08:50
     */
    @RequestMapping(value = "/phone/association/associationDetail")
    public ModelAndView associationDetail(String token, String applyId,String associationId,String associationNewId,String type,String signId) {
        //type =1 为学生 type =2 为老师
        ModelAndView modelAndView = new ModelAndView("/mobile/association/associationDetail");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("associationId", associationId);
        modelAndView.addObject("signId", signId);
        modelAndView.addObject("type", type);
        modelAndView.addObject("associationNewId", associationNewId);
        return modelAndView;
    }

    /**
     * 教师社团列表界面
     * @param token
     * @param applyId
     * @author cuibiming
     * @date 2019/9/9 08:50
     */
    @RequestMapping(value = "/phone/association/teacherAssociationList")
    public ModelAndView teacherAssociationList(String token, String applyId) {
        ModelAndView modelAndView = new ModelAndView("/mobile/association/associationDetail");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        return modelAndView;
    }


    /**
     * 获取社团报名列表(学生)
     * @author hanjun
     * @date 2018/12/20 8:42
     */
//    @BaseLog(module = "社团报名", classz = "社团报名", methods = "社团报名列表", businessType = BusinessType.GET, operatorType= OperatorType.MOBILE)
    @RequestMapping(value = "/phone/association/leftJson")
    public ReturnResult questionJsonList(String token,WxAssociationNewStuVo wxAssociationNewStuVo,String applyId) {
        // 分页
        WxMember wxMember = acceptToken(token);
        WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
        wxEducationUserVo.setMemberId(wxMember.getId());
        List<WxEducationUser> wxEducationUserList = wxEducationUserService.listExample(wxEducationUserVo);
        //能登录进去就可以直接使用
        WxEducationUser wxEducationUser = wxEducationUserList.get(0);
        wxEducationUser.setApplyId(applyId);
        //查询该用户所在招新
        WxAssociationNewVo wxAssociationNew=new WxAssociationNewVo();
        wxAssociationNew.setApplyId(applyId);
        wxAssociationNew.setUserId(wxEducationUser.getId());
//        List<WxAssociationNew> wxAssociationNews=wxAssociationNewService.listByUserId(wxAssociationNew);
//        //CacheUtils.remove(AssassicationDetailUtils.CACHE_DICT_MAP);
//        List<WxAssociationNewDetailVo> detailList=new ArrayList<WxAssociationNewDetailVo>();
//        for(WxAssociationNew w:wxAssociationNews){
//            WxAssociationNewDetailVo wxAssociationNewDetailVo =AssassicationDetailUtils.getAssasicationDetails(w.getId());
//            detailList.add(wxAssociationNewDetailVo);
//        }
        List<WxAssociationNewDetailVo> detailList = wxAssociationNewDetailService.findVoListNew(wxEducationUser);
        PageInfo<WxAssociationNewDetailVo> page = new PageInfo<>(detailList);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 获取社团报名列表(学生)
     * @author hanjun
     * @date 2018/12/20 8:42
     */
//    @BaseLog(module = "社团报名", classz = "社团报名", methods = "我的社团", businessType = BusinessType.GET, operatorType= OperatorType.MOBILE)
    @RequestMapping(value = "/phone/association/rightJson")
    public ReturnResult rightJson(String token,WxAssociationNewStuVo wxAssociationNewStuVo,String applyId) {
        // 分页
        WxMember wxMember = acceptToken(token);
        WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
        wxEducationUserVo.setMemberId(wxMember.getId());
        List<WxEducationUser> wxEducationUserList = wxEducationUserService.listExample(wxEducationUserVo);
        //能登录进去就可以直接使用
        WxEducationUser wxEducationUser = wxEducationUserList.get(0);
        wxEducationUser.setApplyId(applyId);
        List<WxAssociationNewDetailVo> detailList = wxAssociationNewDetailService.findVoListMy(wxEducationUser);
        PageInfo<WxAssociationNewDetailVo> page = new PageInfo<>(detailList);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }



    /**
     * 获取社团详情
     * @author hanjun
     * @date 2018/12/20 8:42
     */
//    @BaseLog(module = "社团报名", classz = "社团报名", methods = "获取社团详情", businessType = BusinessType.GET, operatorType= OperatorType.MOBILE)
    @RequestMapping(value = "/phone/association/getAssociationDetail")
    public ReturnResult questionJsonList(String token,WxAssociationVo wxAssociationVo) {
        //根据id 获取社团详情
        WxAssociation wxAssociation= AssassicationDetailUtils.getAssasications(wxAssociationVo.getId());
//        List<WxAssociation> list = wxAssociationService.listExample(wxAssociationVo);
//        if (list.size()>0){
//            WxAssociation wxAssociation = list.get(0);
//            return new ReturnResult(wxAssociation);
//        }
        //查询报名记录
        WxMember wxMember = acceptToken(token);
        WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
        wxEducationUserVo.setMemberId(wxMember.getId());
        List<WxEducationUser> wxEducationUserList = wxEducationUserService.listExample(wxEducationUserVo);
        //能登录进去就可以直接使用
        WxEducationUser wxEducationUser = wxEducationUserList.get(0);
        //查询是否有报名记录
        WxAssociationSignVo wxAssociationSignVo=new WxAssociationSignVo();
        wxAssociationSignVo.setAssociationId(wxAssociationVo.getId());
        wxAssociationSignVo.setAssociationNewId(wxAssociationVo.getAssociationNewId());
        wxAssociationSignVo.setAssociationUserId(wxEducationUser.getId());
        List<WxAssociationSign> list= wxAssociationSignService.getByAssIdAndAssNewId(wxAssociationSignVo);
        if(list.size()>0){
            wxAssociation.setSignId( list.get(0).getId());
        }else{
            wxAssociation.setSignId("");
        }
        //
        return new ReturnResult(wxAssociation);
    }


    /**
     * 获取教师所带社团
     * @author hanjun
     * @date 2018/12/20 8:42
     */
//    @BaseLog(module = "社团报名", classz = "社团报名", methods = "获取社团详情", businessType = BusinessType.GET, operatorType= OperatorType.MOBILE)
    @RequestMapping(value = "/phone/association/getTeacherAssocaitionList")
    public ReturnResult getTeacherAssocaitionList(String token,WxAssociationVo wxAssociationVo) {


        WxMember wxMember = acceptToken(token);
        WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
        wxEducationUserVo.setMemberId(wxMember.getId());
        List<WxEducationUser> wxEducationUserList = wxEducationUserService.listExample(wxEducationUserVo);
        WxEducationUser wxEducationUser = wxEducationUserList.get(0);
        List<WxAssociationNewDetailVo> detailVoList = new ArrayList<>();
        WxAssociationNewDetailVo wxAssociationNewDetailVo = new WxAssociationNewDetailVo();
        wxAssociationNewDetailVo.setApplyId(wxAssociationVo.getApplyId());
        wxAssociationNewDetailVo.setUserNumber(wxEducationUser.getUserNumber());
        List<WxAssociationNewDetailVo> detailList = wxAssociationNewDetailService.findVoListTeaNew(wxAssociationNewDetailVo);
        PageInfo<WxAssociationNewDetailVo> page = new PageInfo<>(detailList);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());

    }
}
